/**
 *
 * @description:【antd-theme-generator】启动脚本
 * @author:翟磊
 * @date:2022-03-14
 *
 **/

const path = require("path");
const fs = require("fs");
const request = require("request");
let themeVars = require("./default.js");
const { assets_host } = require("../environment/index");
//可能需要修改的less变量
let themeVariables = [];
let lessVarsStr = "",
  rootVars = "";
let themeDatas = {};
Object.keys(themeVars).forEach((key) => {
  themeVariables.push(`@${key}`);
  lessVarsStr += `@${key}:${themeVars[key]};`;
  rootVars += `--${key}:${themeVars[key]};`;
  themeDatas[key] = themeVars[key];
});
lessVarsStr += `@assetsHost:"${assets_host}";`;
rootVars += `--assetsHost:"${assets_host}";`;
//开始生成主题文件
fs.writeFileSync(
  path.join(__dirname, "./compileFiles/defaultTheme.less"),
  `${lessVarsStr}`
);
fs.writeFileSync(
  path.join(__dirname, "./compileFiles/cssVaribles.less"),
  `:root {${rootVars}};:export{${rootVars}}`
);
fs.writeFileSync(
  path.join(__dirname, "./compileFiles/theme.js"),
  `module.exports =${JSON.stringify(themeDatas)}`
);
return;

// const assets_host = "https://jszhgd.cn/static";
// console.log("正在读取主题资源...", path.join(__dirname, "./", "default.json"));
request(path.join(__dirname, "./", "default.json"), (err, response, body) => {
  if (err) {
    console.log("主题资源读取失败", err);
  } else {
    let themeVars = JSON.parse(response.body);
    console.log(`主题资源读取成功==`, `${themeVars}`);
    console.log(`开始解析主题资源文件...`);

    //

    console.log(`开始解析主题资源文件...`);

    console.log(`主题文件创建成功`);
    console.log("开始配置主题");
  }
});
